package org.sport.dbImplement;

import org.sport.dbInterface.LocOperation;
import org.sport.model.entityModel.LocationtableEntity;
import org.sport.model.entityModel.SporthistorytableEntity;
import org.sport.repository.LocationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Allen on 2017/10/11.
 */
@Service
public class LocOpeImp implements LocOperation {



    @Autowired
    LocationRepository locationRepository;



    /**
     * 通过list增加
     *
     * @param track
     */
    @Override
    public boolean addLocationList(List<LocationtableEntity> track) {

    try {
            for (int i = 0 ; i < track.size();i++)
            {
                locationRepository.saveAndFlush(track.get(i));
            }
            return true;
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
            return false;
        }
    }

    /**
     * 增
     *
     * @param locus
     */
    @Override
    public boolean addOneLocation(LocationtableEntity locus) {

        if(locationRepository.saveAndFlush(locus) != null)
            return true;
        else
            return false;
    }

    /**
     * 删,One By One
     *
     * @param locus
     */
    @Override
    public boolean delOneLocation(LocationtableEntity locus) {
        if(locus != null) {
            LocationtableEntity locationtableEntity = new LocationtableEntity();
            locationtableEntity = locationRepository.findOne(locus.getId());
            locationRepository.delete(locationtableEntity);
            return true;
        }
        else
            return false;
    }

    /**
     * 通过查找运动历史记录Id来删除
     *
     * @param spohisid
     * @return
     */
    @Override
    public boolean delLocationBySpoHisId(SporthistorytableEntity spohisid) {

        locationRepository.delBySpoHisId(spohisid);
        if(locationRepository.findBySpoHisId(spohisid) == null)
            return true;
        else
            return false;
    }

    /**
     * 查
     *
     * @param id
     */
    @Override
    public LocationtableEntity findById(int id) {
        return locationRepository.findOne(id);
    }

    /**
     * 返回对应sportid的经纬度list
     *
     * @param spohisid
     */
    @Override
    public List<LocationtableEntity> findBySpoHisId(SporthistorytableEntity spohisid) {
        if(spohisid != null)
        {
            return locationRepository.findBySpoHisId(spohisid);
        }
        else
            return null;
    }
}
